<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
use App\Models\Linkslistsstatistic;
use App\Models\Linkslists;
use App\Models\Linksliststotal;

class Linkslistsstatisticredis extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'links:statisticredis';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '每天0点保存Redis数据统计';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        // 昨天的日期
        $yesterday = date("Ymd",strtotime("-1 day"));
        // 前提的日期
        $beforeyesterday = date("Ymd",strtotime("-3 day"));
        $yesterday_page_views = Redis::hgetall('page_view_'.$yesterday);

        $listsStatistics = [];
        $handlenumber = 1;
        foreach ($yesterday_page_views as $links_id => $page_view) {
            $mini_views = Redis::hget('mini_view_'.$yesterday, $links_id);
            $mini_directs = Redis::hget('mini_direct_'.$yesterday, $links_id);
            $mini_activates = Redis::hget('mini_activate_'.$yesterday, $links_id);
            $mini_card = Redis::hget('mini_card_'.$yesterday, $links_id);
            $mini_view = $mini_views ? $mini_views : 0;
            $mini_direct = $mini_directs ? $mini_directs : 0;
            $mini_activate = $mini_activates ? $mini_activates : 0;
            $mini_card = $mini_card ? $mini_card : 0;

            if (mb_strlen($links_id) >= 32) continue;

            $listsStatistics[] = ["links_id"=>$links_id,"phase"=>$yesterday,"page_view"=>$page_view,"mini_view"=>$mini_view,"mini_direct"=>$mini_direct,"mini_activate"=>$mini_activate,"mini_press"=>$mini_activate,"mini_card"=>$mini_card,"created_at"=>time()];

            // 更新剩余访问量
            Linkslists::updateLinksLeftPv($links_id,$page_view);
            // 更新累计统计
            Linksliststotal::updateLinkListTotal($links_id,$page_view,$mini_view,$mini_direct,$mini_activate,$mini_card);

            $handlenumber += 1;
            if ($handlenumber > 100) {
                Linkslistsstatistic::creatLinksListsStatistic($listsStatistics);
                $handlenumber = 1;
                $listsStatistics = [];
            }
            Redis::hdel('mini_direct_'.$yesterday,$links_id);
        }

        // 再次保存遗漏的mysql
        if (!empty($listsStatistics)) {
            Linkslistsstatistic::creatLinksListsStatistic($listsStatistics);
        }

        // 直接访问的数据
        $yesterday_mini_directs = Redis::hgetall('mini_direct_'.$yesterday);
        $otherListsStatistics = [];
        foreach ($yesterday_mini_directs as $links_id => $mini_direct) {
            $mini_views = Redis::hget('mini_view_'.$yesterday, $links_id);
            $page_views = Redis::hget('page_view_'.$yesterday, $links_id);
            $mini_activates = Redis::hget('mini_activate_'.$yesterday, $links_id);
            $mini_card = Redis::hget('mini_card_'.$yesterday, $links_id);
            $mini_view = $mini_views ? $mini_views : 0;
            $page_view = $page_views ? $page_views : 0;
            $mini_activate = $mini_activates ? $mini_activates : 0;
            $mini_card = $mini_card ? $mini_card : 0;

            if (mb_strlen($links_id) >= 32) continue;
            // 更新剩余访问量
            Linkslists::updateLinksLeftPv($links_id,$mini_direct);
            // 更新累计统计
            Linksliststotal::updateLinkListTotal($links_id,$page_view,$mini_view,$mini_direct,$mini_activate,$mini_card);

            $otherListsStatistics[] = ["links_id"=>$links_id,"phase"=>$yesterday,"page_view"=>$page_view,"mini_view"=>$mini_view,"mini_direct"=>$mini_direct,"mini_activate"=>$mini_activate,"mini_press"=>$mini_activate,"mini_card"=>$mini_card,"created_at"=>time()];
        }
        Linkslistsstatistic::creatLinksListsStatistic($otherListsStatistics);

        // 删除3天以前的数据
        Redis::del('mini_view_'.$beforeyesterday);
        Redis::del('mini_direct_'.$beforeyesterday);
        Redis::del('page_view_'.$beforeyesterday);
        Redis::del('mini_activate_'.$beforeyesterday);
        Redis::del('mini_card_'.$beforeyesterday);
        return "success";
    }
}
